package de.clapp.data;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;

import com.mpatric.mp3agic.ID3v1;
import com.mpatric.mp3agic.ID3v2;
import com.mpatric.mp3agic.InvalidDataException;
import com.mpatric.mp3agic.Mp3File;
import com.mpatric.mp3agic.UnsupportedTagException;

public class MusicItem implements Serializable
{
    private static final long serialVersionUID = 262263675352918921L;

    private String interpret, album, titel, year;

    private int votes;

    private String filePath;

    private int length;

    public MusicItem()
    {
    };

    public MusicItem(File file) throws UnsupportedTagException, InvalidDataException, IOException
    {
        this.filePath = file.getAbsolutePath();
        Mp3File mp3file = new Mp3File(file.getAbsolutePath());

        this.length = (int) (mp3file.getLengthInSeconds());
        if (mp3file.hasId3v2Tag())
        {
            this.readID3v2Tag(mp3file.getId3v2Tag());
            // this.createAlbumImageFromTag(mp3file.getId3v2Tag());
        }
        else if (mp3file.hasId3v1Tag())
        {
            this.readID3v1Tag(mp3file.getId3v1Tag());

        }
    }

    /*
     * private void createAlbumImageFromTag(ID3v2 tag) { try { byte[] imageData
     * = tag.getAlbumImage(); if (imageData != null) { BufferedImage image =
     * ImageIO.read(new ByteArrayInputStream(imageData));
     * 
     * ByteArrayOutputStream baos = new
     * ByteArrayOutputStream(tag.getAlbumImage().length);
     * 
     * ImageIO.write(image, "png", baos); baos.flush(); } } catch (IOException
     * e) { e.printStackTrace(); } /* String
     * base64String=Base64.encode(baos.toByteArray()); baos.close();
     * 
     * byte[] bytearray = Base64.decode(base64String);
     * 
     * BufferedImage imag=ImageIO.read(new ByteArrayInputStream(bytearray));
     * ImageIO.write(imag, "jpg", new File(dirName,"snap.jpg"));
     * 
     * JPEGImageEncoder jpegImageEncoder = JPEGCodec.createJPEGEncoder(new
     * FileOutputStream("MusicImages\\" +tag.getArtist()+ "-" +
     * tag.getAlbum()));
     * 
     * Image scaledImage = image.getScaledInstance(640, 480,
     * Image.SCALE_SMOOTH);
     * 
     * BufferedImage outImg = new BufferedImage(640, 480,
     * BufferedImage.TYPE_INT_RGB); Graphics g = outImg.getGraphics();
     * g.drawImage(scaledImage, 0, 0, null); g.dispose();
     * 
     * 
     * JPEGEncodeParam param =
     * jpegImageEncoder.getDefaultJPEGEncodeParam(outImg);
     * param.setQuality(0.5F, true); jpegImageEncoder.encode(outImg, param);
     * 
     * 
     * int w = bimg.getWidth(), h = bimg.getHeight();
     * 
     * BufferedImage bim2 = new BufferedImage(w, h,
     * BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = (Graphics2D)
     * bim2.createGraphics();
     * 
     * g2.drawImage(bim1, 0, 0, this);
     * 
     * }
     */

    private void readID3v2Tag(ID3v2 tag)
    {
        this.interpret = tag.getArtist();
        this.album = tag.getAlbum();
        this.titel = tag.getTitle();
        this.year = tag.getYear();
    }

    private void readID3v1Tag(ID3v1 tag)
    {
        this.interpret = tag.getArtist();
        this.album = tag.getAlbum();
        this.titel = tag.getTitle();
        this.year = tag.getYear();
    }

    public void incrementVotes()
    {
        this.votes++;
    }

    public void resetVotes()
    {
        this.votes = 0;
    }

    public String getFilePath()
    {
        return filePath;
    }

    public void setFilePath(String filePath)
    {
        this.filePath = filePath;
    }

    public String getInterpret()
    {
        return interpret;
    }

    public void setInterpret(String interpret)
    {
        this.interpret = interpret;
    }

    public String getAlbum()
    {
        return album;
    }

    public void setAlbum(String album)
    {
        this.album = album;
    }

    public String getYear()
    {
        return year;
    }

    public void setYear(String year)
    {
        this.year = year;
    }

    public String getTitel()
    {
        return titel;
    }

    public void setTitel(String titel)
    {
        this.titel = titel;
    }

    public int getVotes()
    {
        return votes;
    }

    public void setVotes(int votes)
    {
        this.votes = votes;
    }

    public int getLength()
    {
        return length;
    }

    public void setLength(int length)
    {
        this.length = length;
    }
}
